define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant.store/index' + location.search,
                    add_url: 'merchant.store/add',
                    edit_url: 'merchant.store/edit',
                    del_url: 'merchant.store/del',
                    multi_url: 'merchant.store/multi',
                    import_url: 'merchant.store/import',
                    table: 'merchant_store',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),visible:false},
                        {field: 'merchant_id', title: __('Merchant_id'),visible:false},
                        {field: 'store_name', title: __('Store_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'user_id', title: __('User_id'),visible:false},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'provinceName', title: __('Province'),operate:false},
                        {field: 'cityName', title: __('City'),operate:false},
                        {field: 'districtName', title: __('District'),operate:false},
                        {field: 'address', title: __('Address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'long', title: __('Long'), operate:'BETWEEN'},
                        {field: 'lat', title: __('Lat'), operate:'BETWEEN'},
                        {field: 'business_licence', title: __('Business_licence'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'tel_phone', title: __('Tel_phone'), operate: 'LIKE'},
                        {field: 'background_image', title: __('Background_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'business_start_hours', title: __('Business_start_hours'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'business_end_hours', title: __('Business_end_hours'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        //{field: 'level', title: __('Level')},
                        {field: 'level_name', title: __('Level'),operate: false},
                        {field: 'organizeable', title: __('Organizeable'), searchList: {"0":__('Organizeable 0'),"1":__('Organizeable 1')}, formatter: Table.api.formatter.normal},
                        {field: 'max_person', title: __('Max_person')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:[
                                {
                                    name:'员工管理',
                                    extend:'data-area=["90%","90%"]',
                                    title:'员工管理',
                                    text:'员工管理',
                                    icon:'fa fa-magic',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'store/employee/index',
                                    refresh:true
                                },
                                {
                                    name:'余额记录',
                                    extend:'data-area=["90%","90%"]',
                                    title:'余额记录',
                                    icon:'fa fa-magic',
                                    text:'余额记录',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'store/money_log/index',
                                    refresh:true
                                },
                                {
                                    name:'邀请统计',
                                    extend:'data-area=["90%","90%"]',
                                    title:'邀请统计',
                                    icon:'fa fa-magic',
                                    text:'邀请统计',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'store/invite_statistics/index',
                                    refresh:true
                                },

                                {
                                    name:'组队统计',
                                    extend:'data-area=["90%","90%"]',
                                    title:'组队统计',
                                    icon:'fa fa-magic',
                                    text:'组队统计',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    url:'store/number_statistics/index',
                                    refresh:true
                                },
                                {
                                    name:'盈利统计',
                                    title:'盈利统计',
                                    icon:'fa fa-magic',
                                    text:'盈利统计',
                                    classname:'btn btn-xs btn-success btn-dialog',
                                    url:'store/profit_statistics/index',
                                    refresh:true
                                }
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("[data-toggle='addresspicker']").data("callback", function(res){
                //console.log(res)
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
